PHP- Simple XML

    This simple XML parser will parse the name, containing attributes and the text content within the simple XML file. These are tree-based parsers. PHP supports some of the XML functions that are described below.

    • simplexml_load_file()- this function has five parameters among which the first parameter is mandatory as it takes the file path to load the XML file.
    simplexml_load_file(($fileName,$class,$options,$ns,$is_prefix)
    • simplexml_load_string()- this function also takes five parameters among which the first parameter is mandatory to pass which is the string rather than the file reference.
    simplexml_load_string($XMLData,$class,$options,$ns,$is_prefix)
    • simplexml_import_dom()- this function will accept the XML content in complex DOM format and allows it to convert the complex XML into simple XML format. This function will take two parameters.
    simplexml_load_string($DOMNode,$class)

    Example:

    <?php
       $data = "<?xml version = '1.0' encoding = 'UTF-8'?>
       <data>
          <Subject>Program</Subject>
          <Content>CS</Content>
          <Price>10</Price>
       </data>";
       $db_xml = simplexml_load_string($data) or die("Error");
    ?>
    <html>
       <head>
          <body>
             <?php
                print_r($db_xml);
             ?>
         </body>
       </head>
    </html>

    Output:

    SimpleXMLElement Object ( [Subject] => Program [Content] => CS [Price] => 10 )

    People are also reading: